﻿using Engine;
using Game;
namespace Mekiasm.Common
{
    public class FluidElement : Element<Interface.IMekFluidMachine>
    {
        public Color RenderColor;

        public FluidElement(int x, int y, int z) : base(x, y, z)
        {
            RenderColor = Color.White;
        }
        public void IncreaseFluid()
        {
            for (int i = 0; i < Connections.Count; i++)
            {
                var connect = Connections[i];
                Point3 point = connect.Point - Point;
                int thisF = Point3ToFace(point);
                int face = OppositeFace(thisF);
                var neiborMachine = connect.Instance.Machine;
                var currentMachine = Instance.Machine;
                neiborMachine.MachineFaceConfig.TryGetValue(face, out MachineFaceConfig config);
                currentMachine.MachineFaceConfig.TryGetValue(thisF, out MachineFaceConfig configF);
                if (config == null) continue;
                if (config.DeviceType == MekBlock.DeviceType.Fluid && config.MachineMode == MekBlock.MachineMode.OUT && configF.DeviceType == MekBlock.DeviceType.Fluid && configF.MachineMode == MekBlock.MachineMode.IN)
                {
                    for (int j = 0; j < currentMachine.Slots.Count; j++)
                    {
                        var currentSlot = currentMachine.Slots[j];

                        if (currentSlot.SlotType.HasFlag(ComponentEnergyMachine.SlotType.IN))
                        {
                            for (int m = 0; m < neiborMachine.Slots.Count; m++)
                            {
                                ComponentEnergyMachine.Slot NeiborSlot = neiborMachine.Slots[m];

                                if (NeiborSlot.SlotType.HasFlag(ComponentEnergyMachine.SlotType.OUT))
                                {
                                    if (NeiborSlot.FuelAmount >= currentMachine.InputFluidAmountLimit)
                                    {
                                        if (currentMachine.AddFuel(j, NeiborSlot.FuelType, currentMachine.InputFluidAmountLimit, NeiborSlot.FuelValue))
                                        {
                                            NeiborSlot.FuelAmount -= currentMachine.InputFluidAmountLimit;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 充电或者放电
        /// </summary>
        /// <param name="chargeSlot">充电位置</param>
        public void ChargeOrDischargeItem(int chargeSlot)
        {
            var currentMachine = Instance.Machine;
            var slot = currentMachine.Slots[chargeSlot];
            int value = slot.Value;
            var block = BlocksManager.Blocks[Terrain.ExtractContents(value)];
            //默认SlotIn
            bool SlotOutputMachine = slot.SlotType.HasFlag(ComponentEnergyMachine.SlotType.Charge_SlotOut);
            if (slot.Count > 0 && block is MekBlock mek)
            {
                if (mek.GetItem(ref value) is Interface.IChargeAbleItem charge && charge.ChargeFuelType == ComponentEnergyMachine.FuelType.Fliud)
                {
                    if (SlotOutputMachine)
                    {
                        int fuelValue = charge.GetChargeFuelValue(value);
                        //格子给机器充电
                        if (charge.GetCurrentAmount(value) >= currentMachine.InputFluidAmountLimit && currentMachine.CanAcceptFuel(chargeSlot, fuelValue) && (slot.FuelAmount == 0 || charge.GetChargeFuelValue(value) == slot.FuelValue) && slot.FuelAmount + currentMachine.InputFluidAmountLimit <= slot.FuelCapacity)
                        {
                            slot.Value = charge.SetCurrentAmount(value,fuelValue, -currentMachine.InputFluidAmountLimit);
                            slot.FuelValue = fuelValue;
                            slot.FuelType = charge.ChargeFuelType;
                            slot.FuelAmount += currentMachine.InputFluidAmountLimit;
                        }
                    }
                    else
                    {
                        //机器给格子充电
                        long amount = charge.GetCurrentAmount(value);
                        if (slot.FuelAmount >= currentMachine.OutputFluidAmountLimit && charge.CanAcceptFuel(slot.FuelValue) && (amount == 0 || charge.GetChargeFuelValue(value) == slot.FuelValue) && amount + currentMachine.OutputFluidAmountLimit <= charge.MaxAmount)
                        {
                            slot.Value = charge.SetCurrentAmount(value, slot.FuelValue, currentMachine.OutputFluidAmountLimit);
                            slot.FuelAmount -= currentMachine.OutputFluidAmountLimit;
                        }

                    }

                }
            }
        }
    }
}
